﻿using System;

namespace BingSpatialDataServiceExcelAddin.BSDSTools.Models
{
    public static class DataTypes
    {
        public const string String = "Edm.String";
        public const string Long = "Edm.Int64";
        public const string Boolean = "Edm.Boolean";
        public const string Double = "Edm.Double";
        public const string DateTime = "Edm.DateTime";

        public static string GetTypeName(Type type)
        {
            if (type == typeof(string) || type == typeof(String))
            {
                return String;
            }
            else if (type == typeof(int) || type == typeof(Int16) || type == typeof(Int32) || type == typeof(Int64) || type == typeof(long))
            {
                return Long;
            }
            else if (type == typeof(bool) || type == typeof(Boolean))
            {
                return Boolean;
            }
            else if (type == typeof(double) || type == typeof(Double))
            {
                return Double;
            }
            else if (type == typeof(DateTime))
            {
                return DateTime;
            }

            return String;
        }

        public static object CastToSafeType(object o, string typeName)
        {
            if (o == null)
            {
                return null;
            }

            switch (typeName)
            {
                case String:
                    return o.ToString();
                case Long:
                    long l;
                    if (long.TryParse(o.ToString(), out l))
                    {
                         return l;
                    }
                    return null;
                case Boolean:
                    bool b;
                    if (bool.TryParse(o.ToString(), out b))
                    {
                         return b;
                    }
                    return null;
                case Double:
                    double d;
                    if (double.TryParse(o.ToString(), out d))
                    {
                        return d;
                    }
                    return null;
                case DateTime:
                    if (o is DateTime && o != null)
                    {
                        DateTime dt = (DateTime)o;
                        return dt.ToString("yyyy-mm-ddThh:mm[:ss[.fffffff]]");
                    }
                    return null;
                default:
                    return o;
            }
        }

        public static Type GetType(string typeName){
            switch (typeName)
            {
                case String:
                    return typeof(string);
                case Long:
                    return typeof(long);
                case Boolean:
                    return typeof(bool);
                case Double:
                    return typeof(double);
                case DateTime:
                    return typeof(DateTime);
                default:
                    return typeof(string);
            }
        }
    }
}
